﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Common.Library.Helper
{
    /// <summary>
    /// 计算机信息助手类
    /// </summary>
    public class ComputerInfoHelper
    {
        /// <summary>
        /// 获取本机IP
        /// </summary>
        /// <returns></returns>
        public static string GetLocalMachineIP()
        {
            ManagementObjectCollection instances = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances();
            string str = "";
            foreach (ManagementObject managementObject in instances)
            {
                if ((bool)managementObject["IPEnabled"] && !(managementObject["IPAddress"] as string[])[0].StartsWith("0") && !str.Contains((managementObject["IPAddress"] as string[])[0]))
                    str = str + "|" + (managementObject["IPAddress"] as string[])[0];
            }
            if (str.Length > 1)
                return str.Substring(1, str.Length - 1);
            return "Unknown IP";
        }

        /// <summary>
        /// 获取本机IP数组
        /// </summary>
        /// <returns></returns>
        public static string[] GetLocalMachineIPs()
        {
            string str = GetLocalMachineIP();
            if (str.Length > 0)
            {
                return str.Split('|');
            }
            return null;
        }

        /// <summary>
        /// 获取计算机名称
        /// </summary>
        /// <returns></returns>
        public static string GetMachineName()
        {
            return Dns.GetHostName();
        }

        /// <summary>
        /// 获取本机的MAC地址
        /// </summary>
        /// <returns></returns>
        public static string[] GetLocalMachineMac()
        {
            ManagementObjectCollection instances = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances();
            string str = "";
            foreach (ManagementObject managementObject in instances)
            {
                if ((bool)managementObject["IPEnabled"])
                    str = str + "|" + managementObject["MacAddress"].ToString();
            }
            if (str.Length > 1)
            {
                str = str.Substring(1, str.Length - 1);
            }
            return str.Split('|');
        }

        /// <summary>
        /// 获取CPU的处理器Id
        /// </summary>
        public static string GetCpuProcessorId()
        {
            string strCpu = null;
            ManagementClass myCpu = new ManagementClass("win32_Processor");
            ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
            foreach (ManagementObject myObject in myCpuConnection)
            {
                strCpu = myObject.Properties["Processorid"].Value.ToString();
                break;
            }
            return strCpu;
        }

        /// <summary>
        /// 获取硬盘序列号
        /// </summary>
        /// <returns></returns>
        public static string GetDiskSerialNumber()
        {
            //创建ManagementObjectSearcher对象
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
            String strHardDiskID = null;//存储磁盘序列号
            //调用ManagementObjectSearcher类的Get方法取得硬盘序列号
            foreach (ManagementObject mo in searcher.Get())
            {
                strHardDiskID = mo["SerialNumber"].ToString().Trim();//记录获得的磁盘序列号
                break;
            }
            return strHardDiskID;
        }

        /// <summary>
        /// 获取主板序列号
        /// </summary>
        /// <returns></returns>
        public static string GetMainboardSerialNumber()
        {
            //主板
            string strbNumber = string.Empty;
            ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_baseboard");
            foreach (ManagementObject mo in mos.Get())
            {
                strbNumber = mo["SerialNumber"].ToString();
                break;
            }
            return strbNumber;
        }
    }
}
